#include "image_api.h"

void JOUI::Jo_UIImage_Create(INT width, INT height, UIimage& dstImg)
{
	dstImg = new UIImage(width, height);
}

void JOUI::Jo_UIImage_CreateFromFile(LPCWSTR file, UIimage& dstImg)
{
	dstImg = new UIImage(file);
}

void JOUI::Jo_UIImage_CreateFromBitmap(LPVOID hBitmap, HPALETTE hPalette, HRESULT fPreAlpha, UIimage& dstImg)
{
	dstImg = new UIImage((HBITMAP)hBitmap, hPalette, fPreAlpha);
}

void JOUI::Jo_UIImage_CreateFromIcon(HICON hIcon, UIimage& dstImg)
{
	dstImg = new UIImage(hIcon);
}

void JOUI::Jo_UIImage_CreateFromMemory(LPVOID data, size_t size, UIimage& dstImg)
{
	dstImg = new UIImage(data, size);
}

void JOUI::Jo_UIImage_CreateFromPngbits(INT nWidth, INT nHeight, BYTE* pbBuffer, UIimage& dstImg)
{
	dstImg = new UIImage(nWidth, nHeight, pbBuffer);
}

void JOUI::Jo_UIImage_CreateFromBitmapWic(LPVOID src_bitmap, UIimage& dstImg)
{
	dstImg = new UIImage((IWICBitmap*)src_bitmap);
}

void JOUI::Jo_UIImage_CreateFromStream(LPSTREAM lpStream, UIimage& dstImg)
{
	dstImg = new UIImage(lpStream);
}

void JOUI::Jo_UIImage_CreateSvg(LPCWSTR svgCode, BOOL fCode, UINT uiWidth, UINT uiHeight, INT color, UIimage& dstImg)
{
	dstImg = new UIImage(svgCode, fCode, uiWidth, uiHeight, color);
}

void JOUI::Jo_UIImage_CreateZip(UIZip* hRes, LPCWSTR fileName, UIimage& dstImg)
{
	dstImg = new UIImage(hRes, fileName);
}

HANDLE JOUI::Jo_UIImage_LoadFromMemoryHandle(LPVOID lpData, size_t dwLen, INT uType, INT nIndex)
{
	return UIImage::LoadFromMemoryHandle(lpData, dwLen, uType, nIndex);
}

HRESULT JOUI::Jo_UIImage_SetCurFrame(UIimage hImage, INT index)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->SetCurFrame(index);
	return S_FALSE;
}

UINT JOUI::Jo_UIImage_GetCurFrame(UIimage hImage)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->GetCurFrame();
	return 0;
}

HRESULT JOUI::Jo_UIImage_GetFrameCount(UIimage hImage, INT& nFrameCount)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
	{
		nFrameCount = lpImg->GetFrameCount();
		return S_OK;
	}
	return S_FALSE;
}

HRESULT JOUI::Jo_UIImage_GetSize(UIimage hImage, UINT& Width, UINT& Height)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->GetSize(Width, Height);
	return S_FALSE;
}

HRESULT JOUI::Jo_UIImage_NextFrame(UIimage hImage, UINT* r_next_index, UINT* r_next_delay)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->NextFrame(r_next_index, r_next_delay);
	return S_FALSE;
}

BOOL JOUI::Jo_UIImage_GetFrameDelay(UIimage hImage, INT& lpDelayAry, INT nFrames)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->GetFrameDelay(lpDelayAry, nFrames);
	return 0;
}

HRESULT JOUI::Jo_UIImage_Lock(UIimage hImage, RECT* lpRectL, DWORD flags, INT PixelFormat, LPVOID& lpLockedBitmapData)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->Lock(lpRectL, flags, PixelFormat, (info_bitmapdata**)lpLockedBitmapData);
	return S_FALSE;
}

HRESULT JOUI::Jo_UIImage_UnLock(UIimage hImage, LPVOID lpLockedBitmapData)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->UnLock((info_bitmapdata*)lpLockedBitmapData);
	return S_FALSE;
}

HRESULT JOUI::Jo_UIImage_GetPixel(UIimage hImage, INT x, INT y, INT& r_color)
{
	UIColor tmp; HRESULT rel = 0;
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		rel = lpImg->GetPixel(x, y, tmp);
	r_color = tmp.GetARGB();
	return rel;
}

HRESULT JOUI::Jo_UIImage_SetPixel(UIimage hImage, INT x, INT y, INT color)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->SetPixel(x, y, color);
	return S_FALSE;
}

HRESULT JOUI::Jo_UIImage_Copy(UIimage hImage, UIimage& r_dest, RECT* copy_rect)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
	{
		UIImage *tmp;
		lpImg->Copy(&tmp, copy_rect);
		r_dest = tmp;
		return S_OK;
	}
	return S_FALSE;
}

HRESULT JOUI::Jo_UIImage_Scale(UIimage hImage, UINT new_width, UINT new_height, UIimage& r_dest, RECT* copy_rect)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
	{
		UIImage *tmp;
		lpImg->Scale(new_width, new_height, &tmp, copy_rect);
		r_dest = tmp;
		return S_OK;
	}
	return S_FALSE;
}

BOOL JOUI::Jo_UIImage_Rotateflip(UIimage hImage, INT rfType, UIimage& r_dest)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->Rotateflip(rfType, (UIImage**)r_dest);
	return FALSE;
}

LPVOID JOUI::Jo_UIImage_GetContext(UIimage hImage)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->GetContext();
	return 0;
}

BOOL JOUI::Jo_UIImage_SaveToMemory(UIimage hImage, LPVOID& lpBuffer, size_t& len, INT format)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->Save(lpBuffer, len, format);
	return FALSE;
}

BOOL JOUI::Jo_UIImage_SaveToFile(UIimage hImage, LPCWSTR wzFileName, INT format)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
		return lpImg->Save(wzFileName, format);
	return FALSE;
}

HRESULT JOUI::Jo_UIImage_Destroy(UIimage hImage)
{
	auto lpImg = (UIImage*)hImage;
	if (lpImg)
	{
		delete lpImg;
		return S_OK;
	}
	return S_FALSE;
}
